<?php

namespace App\Model;
/**
 * lzy
 * Class AuthRule
 * @package App\Model
 */
class  AuthRule extends BaseModel
{
    const RULE_URL = 1;   //子菜单
    const RULE_MAIN = 2; //主菜单


    /**
     * 菜单模型
     * @return Menu
     */
    public function getMenuModel()
    {
        return new Menu();
    }

    /**
     * 获取权限列表
     * @param $where
     * @param string $field
     * @return array
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function getAuthRuleList($where, $field = '*')
    {
        return $this->getAll($where, $field);
    }

    /**
     * 更新菜单
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateNodeByMenu($data)
    {

        //获取菜单列表。加入到角色表中,更新与删除
        $module=$data['module'];
        $nodes = $this->getMenuModel()->getReturnNode(false, $data);

        //需要更新和删除的节点必然位于$rules
        $rules = $this->getAll($this->analysisWhere(['module' => 'admin', 'type|in' => [1, 2]]));
        $data = array(); //保存需要插入和更新的新节点
        foreach ($nodes as $value) {
            $temp['name'] = $value['url'];
            $temp['title'] = $value['title'];
            $temp['module'] =$module;
            if ($value['pid'] > 0) {
                $temp['type'] = AuthRule::RULE_URL;
            } else {
                $temp['type'] = AuthRule::RULE_MAIN;
            }
            $temp['status'] = 1;
            $data[strtolower($temp['name'] . $temp['module'] . $temp['type'])] = $temp; //去除重复项
        }

        $update = array(); //保存需要更新的节点
        $ids = array(); //保存需要删除的节点的id
        foreach ($rules as $index => $rule) {
            $key = strtolower($rule['name'] . $rule['module'] . $rule['type']);
            if (isset($data[$key])) {//如果数据库中的规则与配置的节点匹配,说明是需要更新的节点
                $data[$key]['id'] = $rule['id']; //为需要更新的节点补充id值
                $update[] = $data[$key];
                unset($data[$key]);
                unset($rules[$index]);
                unset($rule['condition']);
                $diff[$rule['id']] = $rule;
            } elseif ($rule['status'] == 1) {
                $ids[] = $rule['id'];
            }
        }
        if (count($update)) {
            foreach ($update as $k => $row) {
                if ($row != $diff[$row['id']]) {
                    $this->updateOrCreateData($row, array('id' => $row['id']));
                }
            }
        }
        if (count($ids)) {
            $this->editData($this->analysisWhere(['id|in' => $ids]), ['status' => -1]);
            //删除规则是否需要从每个用户组的访问授权表中移除该规则?
        }
        if (count($data)) {
            $this->addDataMuti(array_values($data));
        }
        return true;
    }


    public function getAuthRuleListNameByArray($where){
        return $this->getFiledColumnValue($this->analysisWhere($where),"name",'id');
    }


}
